package com.wfuhui.modules.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wfuhui.common.utils.R;
import com.wfuhui.modules.api.annotation.LoginUser;
import com.wfuhui.modules.oms.entity.UserCouponEntity;
import com.wfuhui.modules.oms.entity.UserEntity;
import com.wfuhui.modules.oms.service.UserCouponService;
import com.wfuhui.modules.oms.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/user")
@Api(value="ApiUser", description="用户接口")
public class ApiUserController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private UserCouponService userCouponService;
	
    @PostMapping("update")
    @ApiOperation(value = "更新用户")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", dataType="string", name = "token", value = "请求header里面增加token", required = true),
    })
    public R update(@LoginUser UserEntity token, @RequestBody UserEntity user){
    	user.setId(token.getId());
    	userService.update(user);
        return R.ok();
    }
    
    @GetMapping("info")
    @ApiOperation(value = "用户信息")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", dataType="string", name = "token", value = "请求header里面增加token", required = true),
    })
    public R info(@LoginUser UserEntity token) {
    	UserEntity userInfo = userService.queryObject(token.getId());
    	return R.ok().put("userInfo", userInfo);
    }
    
    @GetMapping("coupon")
    @ApiOperation(value = "用户优惠券")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", dataType="string", name = "token", value = "请求header里面增加token", required = true),
        @ApiImplicitParam(paramType = "query", dataType="int", name = "status", value = "状态：1未使用，2已使用，3已过期", required = true)
    })
    public R coupon(@LoginUser UserEntity token, Integer status) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("userId", token.getId());
    	map.put("status", status);
    	List<UserCouponEntity> userCouponList = userCouponService.queryList(map);
    	return R.ok().put("userCouponList", userCouponList);
    }
}
